package im.eventservice;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



import im.general.Config;
import jade.core.behaviours.CyclicBehaviour;
import java.io.IOException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Vector;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
//import java.util.logging.SimpleFormatter;

/**
 *
 * @author Giovanni
 */
public class EventServiceLogBehaviour extends CyclicBehaviour{

     private static Logger log = Logger.getLogger("Event Service");
     private static FileHandler fh;
     private static OurFormatter sf = new OurFormatter();
     private EventServiceAgent es;



    public EventServiceLogBehaviour(EventServiceAgent es) throws IOException{
        this.es = es;
         fh = new FileHandler(Config.LOGGING_DIRECTORY+"\\"+Config.EVENT_SERVICE_LOG_FILE);
         fh.setFormatter(sf);
         log.addHandler(fh);
         log.setLevel(Level.ALL);
    }

    public int attachedNodeCounter(){
        int ret=0;

        //es.heads tutti i cluster heads nella rete
        es.getCHList().size();


        return ret;

    }

    public String getTimeStampNow(){

        Calendar cal = new GregorianCalendar();
        int giorno = cal.get(Calendar.DAY_OF_MONTH);
        int mese = cal.get(Calendar.MONTH);
        int anno = cal.get(Calendar.YEAR);
        int ora = cal.get(Calendar.HOUR);
        int minuti = cal.get(Calendar.MINUTE);
        int secondi = cal.get(Calendar.SECOND);
        String orario;

        if (cal.get(Calendar.AM_PM) == 0) {
            orario = "A.M.";
        } else {
            orario = "P.M.";
        }

        return ""+giorno + "-" + (mese + 1) + "-" + anno + " " + ora + ":" + minuti + ":" + secondi + " " + orario+"";

    }

    @Override
    public void action() {
        //throw new UnsupportedOperationException("Not supported yet.");

        //mettiamo il log
        //stampiamo il numero dei cluster head etc etc...
       
        log.log(Level.ALL,""+getTimeStampNow()+";"+es.heads.size()+";"+es.getResourceStatus().getStatusIndex()+";"+es.getResourceStatus().isPlugged()+";"+es.getResourceStatus().getChargeReminderMah()+";"+es.getResourceStatus().getFreeBWKbps()+";"+es.getResourceStatus().getFreeCPUMhz()+";"+es.getResourceStatus().getFreeDiskMB()+";"+es.getResourceStatus().getFreeRamMB()+";"+"status_idx/n_nodi_attaccati"+";"+"band/r_attached"+";");
        

        block(5000);

    }


}
